<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

class Task extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('task')
            //增加一个命令参数
            ->setDescription('the task command')
            ->addArgument('action', Argument::OPTIONAL, "action", '')
            ->addArgument('force', Argument::OPTIONAL, "force", '');
    }

    protected function execute(Input $input, Output $output)
    {
        //获取输入参数
        $action = trim($input->getArgument('action'));
        $force = trim($input->getArgument('force'));

        $task = new \EasyTask\Task();
        // 设置常驻内存
        $task->setDaemon(true);

        // 设置项目名称
        $task->setPrefix('DouYin_GET_ORDER');
        $task->setRunTimePath('./runtime/');
        // 配置任务，每隔600秒访问1次网站 60*10

        $task->addClass(\app\common\task\Task::class, "refreshToken", "refreshToken", 60*20,1);

        // 根据命令执行
        if ($action == 'start')
        {
            $task->start();
        }
        elseif ($action == 'status')
        {
            $task->status();
        }
        elseif ($action == 'stop')
        {
            $force = ($force == 'force'); //是否强制停止
            $task->stop($force);
        }
        else
        {
            exit('Command is not exist');
        }
    }
}
